/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rfid;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author xandru
 */
public class RFIDLogger implements RFIDEventListener{

    String path;
    PrintWriter os;
    SimpleDateFormat format=new SimpleDateFormat("dd/MM/yyyy HH:mm:ss,S");
    public RFIDLogger(String path) throws FileNotFoundException, IOException
    {
          this.path=path;
          init();
    }

    public RFIDLogger() throws FileNotFoundException, IOException
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        this.path = "rfid" + sdf.format(new Date()) + ".log";
        init();
    }

    private void init() throws IOException
    {
        boolean append = true;
        os = new PrintWriter(new FileWriter(new File(path),append));
    }

    public void onNewTag(RFIDTagEvent e) {
        log(e.getTagData());
    }

    public void onUpdateTag (RFIDTagEvent e) {
        log(e.getTagData());
    }

    private void log(TagData e)
    {
        os.print(e.getEPCParameter() + "|");
        os.print(e.getAntennaID() + "|");
        os.print(e.getChannelIndex() + "|");
        os.print(format.format(new Date(e.getFirstSeenTimestampUTC())) + "|");
        os.print(format.format(new Date(e.getLastSeenTimestampUTC()))+ "|");
        os.print(e.getPeakRSSI()+ "|");
        os.print(e.getTagSeenCount()+ "|");
        os.print(e.getTypeNum()+ "|");
        os.println();
        os.flush();
    }

    public void close()
    {
        os.close();
    }
}
